﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {
	public static GameManager instance;
	[SerializeField] GameStage currGameStage;
	public static GameStage gameStage
	{
		get{ return instance.currGameStage;}
		set
		{
			if (instance.currGameStage != value) {
				instance.currGameStage = value;
				if(OnGameStageChange!=null)
				{
					OnGameStageChange(instance.currGameStage);					
				}
			}
		}
	}
	public delegate void OnGameStageDelegate(GameStage stage);
	public static OnGameStageDelegate OnGameStageChange;
	[SerializeField] PlayerController player;
	public static PlayerController Player
	{
		get { return instance.player;}
	}
	public static bool IsPlaying
	{
		get { return instance.currGameStage == GameStage.PLAY || instance.currGameStage == GameStage.RESUME;}
	}
	public static int CurrBall
	{
		get{ return PlayerPrefs.GetInt ("ball");}
		set
		{
			PlayerPrefs.SetInt ("ball", value);
		}
	}
	public static int CurrMap
	{
		get{ return PlayerPrefs.GetInt ("map");}
		set
		{
			PlayerPrefs.SetInt ("map", value);
		}
	}
	private void Awake()
	{
		instance = this;
		OnGameStageChange += OnGameStageChanged;
	}
	private void Start()
	{
		gameStage = GameStage.WAIT;
	}

	private void Update()
	{
		if(Input.GetMouseButtonDown(1))
			{
			PlayerPrefs.DeleteAll ();
		}
	}
	private void OnGameStageChanged(GameStage stage)
	{
		switch (stage) {
		case GameStage.WAIT:
			Wait ();
			break;
		case GameStage.PLAY:
			Play ();
			break;
		case GameStage.PAUSE:
			Pause ();
			break;
		case GameStage.RESUME:
			Resume ();
			break;
		case GameStage.GAMEOVER:
			GameOver ();
			break;
		default:
			break;
		}
	}
	private void GameOver()
	{
		UiManager.GameOver ();
		AdsControl.Instance.showAds ();
	}
	private void Pause()
	{
		Time.timeScale = 0;
		UiManager.PauseOrSound ();
		AdsControl.Instance.showAds ();
	}
	private void Resume()
	{
		Time.timeScale = 1;
		UiManager.Resume ();
	}
	private void Wait()
	{
		BattleGroundHelper.SetBackground ();
		UiManager.InItText ();
		UiManager.Home ();
		Player.Wait ();
		AudioManager.PlayBackground ();
	}
	private void Play()
	{
		Player.PlayGame ();
		LevelManager.StartGame ();
		BattleGroundHelper.SetBackground ();
		AudioManager.PlayBackground ();
		AudioManager.PlayOneShoot (0);
		AudioManager.HideDisableIcon ();
	}
}
public enum GameStage
{
	INIT,
	WAIT,
	PLAY,
	PAUSE,
	RESUME,
	GAMEOVER
}